/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImageContainer.java
 *
 * Created on 27 sept. 2009, 18:58:02
 */

package jvgallery;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

/**
 *
 * @author guillaume
 */
public class ImageContainer extends javax.swing.JPanel {

    private BufferedImage image;
    private String imgName;
    int x,y;
    int height,width;

    /**
     * Constructor
     * @param imgpath Path to image
     */
    public ImageContainer(String imgpath) {
        super();

        // Image position inside container
        this.x = 0;
        this.y = 0;

        // Load image
        BufferedImage img = this.loadImage(imgpath);
        this.image = img;

        // Set size
        this.setSize(img.getWidth(), img.getHeight());

        // DEBUG
        System.out.println("IMAGE SIZE : " + img.getWidth() + " x " + img.getHeight());

        // Finish the job
        initComponents();
        this.setVisible(true);
    }

    /**
     * Returns image height
     * @return int height
     */
    public int getHeight()
    {
        return this.image.getHeight();
    }

    /**
     * Returns image width
     * @return int width
     */
    public int getWidth()
    {
        return this.image.getWidth();
    }

    public String getName()
    {
        return this.imgName;
    }

    /**
     * Load an image from its path on the filesystem
     * @param imgpath
     * @return
     */
    private BufferedImage loadImage(String imgpath)
    {
        BufferedImage bi = null;
        try
        {
            File imgf = new File(imgpath);
            this.imgName = imgf.getName();
            bi = ImageIO.read(imgf);
        }
        catch (Exception e) { e.printStackTrace(); }
        return bi;
    }

     /**
     * Draw image whatever happens.
     * @param g
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, x, y, null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
